package com.milhouz.wormdroid.data.model;

import com.milhouz.wormdroid.gl.factory.TeamFactory;
import com.milhouz.wormdroid.gl.model.BotGLSprite;
import com.milhouz.wormdroid.gl.model.GLSprite;
import com.milhouz.wormdroid.gl.model.hud.HudData;
import com.milhouz.wormdroid.util.StaticValues;

/**
 * <b>Bean managing the game data :</b>
 * <ul>
 * <li>Teams data : team name, nb bots, team power</li>
 * <li>HUD data</li>
 * <li>Bots data</li>
 * </ul>
 */
public class GameData extends AbstractGameData {

	/** HUD Data */
	public HudData mHud;
	private TeamData[] mTeams;
	private BotData[] mBots;

	private int mTeamCount;
	private int mBotCount;
	private int mRoundTime;
	private int mScreenWidth;
	private int mScreenHeight;

	public int currentTeam;
	public int currentBotId;
	public String currentWeapon = StaticValues.DEFAULT_WEAPON;

	public GameData(int nbTeams, int nbBots, int roundTime) {
		this.mTeamCount = nbTeams;
		this.mBotCount = nbBots;
		this.mRoundTime = roundTime;
	}

	public void init(int witdh, int height) {
		mScreenWidth = witdh;
		mScreenHeight = height;
		currentTeam = 0;
		currentBotId = 0;
		// Init Teams
		this.mTeams = new TeamData[mTeamCount];
		for (int i = 1; i <= mTeamCount; i++) {
			mTeams[i - 1] = new TeamData(i, DEFAULT_TEAM_NAME + i, mBotCount);
		}
		// Init Bots
		this.mBots = new BotData[mTeamCount * mBotCount];
		for (int i = 0; i < (mTeamCount * mBotCount); i++) {
			mBots[i] = new BotData(i, DEFAULT_BOT_NAME + i, DEFAULT_POWER);
		}
		// Init HUD
		mHud = new HudData(mScreenWidth, mScreenHeight, mRoundTime, mTeams, mBots[currentBotId]);

	}

	public boolean isEndOfRound() {
		return mHud.isEndOfRound();
	}

	public void setSelectedBotHud(BotGLSprite botSprite) {
		currentBotId = botSprite.getId();
		currentTeam = botSprite.getTeamId();
		botSprite.isViewCentered = true;
		botSprite.isSelectedBot = true;
		mHud.setSelectedBotHud(mBots[currentBotId]);
	}

	public int getNextBotId() {
		if ((currentBotId + 1) < (mTeamCount * mBotCount))
			// TODO Determinate the next bot following its team
			return currentBotId + 1;
		else
			return 0;
	}

	public GLSprite[] generateBotSprites() {
		return TeamFactory.getInstance().generateTeamsBots(mTeamCount, mBotCount);
	}

}